package com.jf.spring.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-22
 * Time:21:53
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * 在bean的init-method前调用
     * @param bean 在IOC容器中创建/配置的bean
     * @param beanName IOC容器中创建/配置的bean的id
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization()  bean = " + bean + "  beanId = " + beanName);
        if (bean instanceof Cat) {
            ((Cat) bean).setName("bob");
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    /**
     * 在bean的init-method后调用
     * @param bean 在IOC容器中创建/配置的bean
     * @param beanName IOC容器中创建/配置的bean的id
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization()  bean = " + bean + "  beanId = " + beanName);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
